import geopandas as gpd

# 1. 定义 pr_name 到 overall_accuracy 的映射字典
oa_mapping = {
    "河南省": 0.6978608452026819,
    "贵州省": 0.6719346482707368,
    "宁夏回族自治区": 0.8036221138709752,
    "天津市": 0.7645549757822986,
    "湖南省": 0.700273408858045,
    "江苏省": 0.7360165859331476,
    "上海市": 0.773156635033095,
    "云南省": 0.7032733787306553,
    "青海省": 0.7085964895260664,
    "吉林省": 0.7217217110074664,
    "甘肃省": 0.7136701351152207,
    "河北省": 0.67532543140282,
    "内蒙古自治区": 0.7497358661319288,
    "浙江省": 0.7157119309555016,
    "湖北省": 0.6875329547871717,
    "北京市": 0.6769840689916758,
    "陕西省": 0.6864027744067754,
    "安徽省": 0.7648828503412318,
    "新疆维吾尔自治区": 0.7188125408863953,
    "江西省": 0.7065831267244042,
    "重庆市": 0.759235195324959,
    "海南省": 0.7247232452598349,
    "黑龙江省": 0.742794140037421,
    "广西壮族自治区": 0.649837278190307,
    "福建省": 0.6449147516625344,
    "山西省": 0.7244224977837797,
    "辽宁省": 0.7384200514222395,
    "香港特别行政区": 0.721875425401235,
    "四川省": 0.7478652305688851,
    "山东省": 0.7314603098430443,
    "广东省": 0.555966039471371,
    "西藏自治区": 0.6237249980576044,
    "台湾省":0.10578062374500506,
    "澳门特别行政区":0.5604933586282342
}

# 2. 读取 Shapefile
shapefile_path = "/home/wangyu/data/nature_data/province.shp"  # 替换为您的 Shapefile 路径
gdf = gpd.read_file(shapefile_path)

# 3. 检查 pr_name 字段中的唯一值（可选）
print("Shapefile 中的 pr_name 值：")
print(gdf['pr_name'].unique())

# 4. 添加 'oa' 字段
gdf['oa'] = gdf['pr_name'].map(oa_mapping)

# 5. 检查是否有未匹配的 pr_name（可选）
missing_oa = gdf[gdf['oa'].isnull()]['pr_name'].unique()
if len(missing_oa) > 0:
    print("以下 pr_name 未在 OA 映射中找到对应的 overall_accuracy：")
    print(missing_oa)
else:
    print("所有 pr_name 都成功映射到了 oa 字段。")

# 6. 保存更新后的 Shapefile
output_shapefile_path = "/home/wangyu/data/nature_data/province_oa.shp"  # 指定输出路径
gdf.to_file(output_shapefile_path)

print(f"更新后的 Shapefile 已保存到 {output_shapefile_path}")
